package com.mm.serivce.note.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.base.lunar.Solar;
import com.mm.domain.common.DCommonCode;
import com.mm.domain.note.DNote;
import com.mm.mapper.note.NoteMapper;
import com.mm.serivce.note.DNoteService;
import com.mm.serivce.common.IDCommonCodeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 描述:
 *
 * @author: yanglin
 * @Date: 2021-05-12-13:50
 * @Version: 1.0
 */
@Service
public class DNoteServiceImpl extends ServiceImpl<NoteMapper, DNote> implements DNoteService {

    private final NoteMapper noteMapper;

    private final IDCommonCodeService codeService;

    public DNoteServiceImpl(NoteMapper noteMapper, IDCommonCodeService codeService) {
        this.noteMapper = noteMapper;
        this.codeService = codeService;
    }


    @Override
    public Page<DNote> queryMyNoteList(Integer page, Integer limit, String text, long startTime, long endTime) {
        Page<DNote> notePage = new Page<>(page, limit);
        LambdaQueryWrapper<DNote> queryWrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(text)) {
            queryWrapper.like(DNote::getText, text);
        }
        // 时间
        DateTime start1 = DateUtil.date(startTime);
        DateTime end1 = DateUtil.date(endTime);
        queryWrapper.ge(DNote::getCreateTime, start1);
        queryWrapper.le(DNote::getCreateTime, end1);
        queryWrapper.eq(DNote::getDelFlg, 0);
        notePage = noteMapper.selectPage(notePage, queryWrapper);
        notePage.setTotal(notePage.getRecords().size());
        return notePage;
    }

    @Override
    public boolean remove(Integer nId) {
        boolean flag = false;
        DNote DNote = noteMapper.selectById(nId);
        if (Objects.nonNull(DNote)) {
            DNote.setDelFlg(1);
            Integer result = noteMapper.updateById(DNote);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }


    @Override
    public List<DNote> queryMyNote(DateTime startTime, DateTime endTime) {
        LambdaQueryWrapper<DNote> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.ge(DNote::getCreateTime, startTime);
        queryWrapper.le(DNote::getCreateTime, endTime);
        queryWrapper.eq(DNote::getDelFlg, 0);
        queryWrapper.orderByAsc(DNote::getCreateTime);
        List<DNote> DNotes = noteMapper.selectList(queryWrapper);
        return DNotes;
    }


    @Override
    public String queryMyNoteWeek() {
        Date time = new Date();
        List<DNote> DNotes = this.queryMyNote(DateUtil.beginOfWeek(time), DateUtil.endOfWeek(time));
        StringBuilder sb = new StringBuilder();
        sb.append(" <p> ").append("周末愉快!&nbsp;Alone.");
        sb.append(" <br> ");
        sb.append(" <br> ");
        Solar date = new Solar();
        sb.append(" <p> ").append("[阳历]");
        sb.append(" <p> ").append(date.toFullString()).append(" <p> ");
        sb.append(" <p> ").append(" <br> ");
        if (DNotes.size() > 0){
            sb.append(" <p> ").append("[这是一周的工作]").append(" <br> ");
            for (DNote DNote : DNotes) {
                // 时间格式化 判断周几
                sb.append(" <p> ").append(this.dayOfWeek(DNote.getCreateTime())).append("&nbsp;&nbsp;")
                        .append(DateUtil.format(DNote.getCreateTime(), "yyyy-MM-dd"));
                sb.append(" <p> ").append(DNote.getText());
                sb.append(" <p> ").append(" <br> ");
            }
        }
        sb.append(" <p> ").append(" <br> ");
        sb.append(getRandomImg());
        sb.append(" <p> ").append(" <br> ");
        sb.append(" <p> ").append("[每周都要总结哦！]");
        return sb.toString();
    }


    /**
     * 返回星期几
     * @param date
     * @return
     */
    public String dayOfWeek(Date date) {
        int day = DateUtil.dayOfWeek(date);
        switch (day) {
            case 1:
                return "星期天";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
            default:
                return "";
        }
    }

    /**
     * 二次元图片API
     *
     * https://t.lizi.moe/pc
     * http://www.98qy.com/sjbz/api.php
     * https://sex.nyan.xyz/api/v2/img
     * https://img.xjh.me/random_img.php
     * https://www.dmoe.cc/random.php
     * https://api.vvhan.com/api/girl
     */
    private List<String> newImages(){
        List imgUrls = new ArrayList<>();
        imgUrls.add("https://t.lizi.moe/pc");
        imgUrls.add("http://www.98qy.com/sjbz/api.php");
        imgUrls.add("https://sex.nyan.xyz/api/v2/img");
        imgUrls.add("https://img.xjh.me/random_img.php");
        imgUrls.add("https://www.dmoe.cc/random.php");
        imgUrls.add("https://api.vvhan.com/api/girl");
        imgUrls.add("https://service-dn725wcs-1306777571.gz.apigw.tencentcs.com/release/");
        imgUrls.add("https://img.paulzzh.com/touhou/random");
        imgUrls.add("https://api.ghser.com/random/api.php");
        return imgUrls;
    }
    public String getRandomImg(){
        List<String> imageUrls = newImages();
        DCommonCode commonCode = codeService.getById(3);
        if (Objects.nonNull(commonCode) && StrUtil.isNotBlank(commonCode.getContent())) {
            imageUrls = (List<String>) Convert.toList(commonCode.getContent());
        }
        StringBuilder sb = new StringBuilder();
        String imgUrl = imageUrls.get(RandomUtil.randomInt(imageUrls.size()));
        sb.append(" <img src="+imgUrl+" alt=\"二次元\" /> ");
        return sb.toString();
    }
}
